﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Sekretar.Repository;

namespace Sekretar
{
    public partial class GodisnjiRecView : Common.Forms.RecView
    {
        int zaposleniId, id;

        RGodisnji rGodisnji = new RGodisnji();

        #region Refresh
        private void RefreshGodisnji()
        {
            rGodisnji = new RGodisnji();

            int index1 = gridView1.TopRowIndex;
            int focusedRow1 = gridView1.FocusedRowHandle;

            bsMaster.DataSource = rGodisnji.Izmeni(this.id);
            gridView1.FocusedRowHandle = focusedRow1;
            gridView1.TopRowIndex = index1;

        }
        #endregion

        public GodisnjiRecView(int zaposleniId, int id)
        {
            InitializeComponent();
            this.zaposleniId = zaposleniId;
            this.id = id;
        }

        #region Load
        private void GodisnjiRecView_Load(object sender, EventArgs e)
        {
            if (id == 0)
            {
                bsMaster.DataSource = rGodisnji.Nov();
                ((DataLayer.Godisnji)bsMaster.Current).ZaposleniId = this.zaposleniId;
                btnNov.Enabled = false;
                btnIzmeni.Enabled = false;
                btnBrisi.Enabled = false;
                btnStampaj.Enabled = false;
                gridControl1.Enabled = false;
            }
            else
                bsMaster.DataSource = rGodisnji.Izmeni(this.id);
        }
        #endregion

        #region OK
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Index = rGodisnji.Snimi((DataLayer.Godisnji)bsMaster.Current);
            }
            catch (Exception ex)
            {
                Common.Tools.MessageError.Show(ex.Message);
                this.DialogResult = DialogResult.None;
            }
        }
        #endregion

        #region Nov
        private void btnNov_Click(object sender, EventArgs e)
        {
            GodisnjiPeriodRecView f = new GodisnjiPeriodRecView(((DataLayer.Godisnji)bsMaster.Current).GodisnjiId, 0);
            if (f.ShowDialog() == DialogResult.OK)
                RefreshGodisnji(); 
        }
        #endregion

        #region Izmeni
        private void btnIzmeni_Click(object sender, EventArgs e)
        {
            GodisnjiPeriodRecView f = new GodisnjiPeriodRecView(((DataLayer.Godisnji)bsMaster.Current).GodisnjiId, ((DataLayer.GodisnjiPeriod)bsGodisniPeriod.Current).GodisnjiPeriodId);
            if (f.ShowDialog() == DialogResult.OK)
                RefreshGodisnji();
        }
        #endregion
    }
}
